package com.apex.sdk.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.apex.bluetooth.callback.DevicePwdCallback;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleDevicePwd;
import com.apex.bluetooth.model.EABleSosContact;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityLockScreenBinding;
import com.apex.sdk.dialog.WaitingDialog;

public class LockScreenActivity extends AppCompatActivity {
    final String TAG = this.getClass().getSimpleName();
    ActivityLockScreenBinding activityLockScreenBinding;
    WaitingDialog waitingDialog;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x11) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                EABleDevicePwd eaBleDevicePwd = (EABleDevicePwd) msg.obj;
                if (eaBleDevicePwd != null) {
                    if (!TextUtils.isEmpty(eaBleDevicePwd.getPwd())) {
                        activityLockScreenBinding.pwd.setText(eaBleDevicePwd.getPwd());
                    }
                }
            } else if (msg.what == 0x12) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(LockScreenActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x13) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                boolean result = (boolean) msg.obj;
                if (!result) {
                    Toast.makeText(LockScreenActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
                }
            } else if (msg.what == 0x14) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(LockScreenActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLockScreenBinding = ActivityLockScreenBinding.inflate(getLayoutInflater());
        setContentView(activityLockScreenBinding.getRoot());
        activityLockScreenBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityLockScreenBinding.tool);
        getSupportActionBar().setTitle(null);
        activityLockScreenBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(LockScreenActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.device_pwd, new DevicePwdCallback() {
                @Override
                public void devicePwdInfo(EABleDevicePwd eaBleDevicePwd) {
                    if (eaBleDevicePwd != null) {
                        Log.e(TAG, "查询锁屏密码成功:" + JSON.toJSONString(eaBleDevicePwd));
                        Message message = new Message();
                        message.what = 0x11;
                        message.obj = eaBleDevicePwd;
                        if (mHandler != null) {
                            mHandler.sendMessage(message);
                        }
                    }

                }

                @Override
                public void mutualFail(int i) {
                    Log.e(TAG, "查询锁屏密码失败");
                    if (mHandler != null) {
                        Message message = new Message();
                        message.what = 0x12;
                        mHandler.handleMessage(message);
                    }
                }
            });
        }
        activityLockScreenBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = activityLockScreenBinding.pwd.getText().toString();
                if (!TextUtils.isEmpty(pwd)) {
                    if (pwd.length() < 6) {
                        Toast.makeText(LockScreenActivity.this, "The password is 6 digits", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(LockScreenActivity.this);
                    }
                    waitingDialog.show();
                    EABleDevicePwd eaBleDevicePwd = new EABleDevicePwd();
                    eaBleDevicePwd.setPwdLen(pwd.length());
                    eaBleDevicePwd.setPwd(pwd);
                    eaBleDevicePwd.setSw(0);//是否启用锁屏,1:开启锁屏,0:关闭锁屏
                    EABleManager.getInstance().addDevicePwd(eaBleDevicePwd, new GeneralCallback() {
                        @Override
                        public void result(boolean b) {
                            Log.e(TAG, "添加锁屏密码成功");
                            if (mHandler != null) {
                                Message message = new Message();
                                message.what = 0x13;
                                message.obj = b;
                                mHandler.sendMessage(message);
                            }
                        }

                        @Override
                        public void mutualFail(int i) {
                            Log.e(TAG, "添加锁屏密码失败");
                            if (mHandler != null) {
                                Message message = new Message();
                                message.what = 0x14;
                                message.obj = i;
                                mHandler.sendMessage(message);
                            }
                        }
                    });

                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
